function readLocalFile(){
    var f = air.File.applicationDirectory.resolvePath( "LocalFile.txt" );
	
    var fs = new air.FileStream();
    fs.open( f, air.FileMode.READ );
    var content = fs.readUTFBytes( fs.bytesAvailable );
    fs.close();
    
    return content;
}
			
function openInBrowser( url ) {
	air.navigateToURL( new air.URLRequest( url ) ); 
}

function detect( text ){
    var url = 'http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=' + text;
	var req = new XMLHttpRequest();
   	req.onreadystatechange = function() {
   		if ( req.readyState == 1 ) {
			document.getElementById( 'translation' ).value = "Loading...";
		}
        if ( req.readyState == 4 ) {
			var res = eval( '(' + req.responseText + ')' );
			if ( res.responseData.language ) {
				document.getElementById( 'srcLang' ).value = res.responseData.language;
				translateIt( text, res.responseData.language, document.getElementById( 'destLang' ).value );
			}
        }
    };
    req.open( 'GET', url, true );
	req.setRequestHeader( 'Referer', 'http://www.dhruvasagar.net/' );
    req.send( null );
}

function translate() {
	var text = document.getElementById( 'text' ).value;
	var srcLang = document.getElementById( 'srcLang' ).value;
	var destLang = document.getElementById( 'destLang' ).value;
    if ( srcLang == "detect" ) {
		detect( text );
	} else {
		translateIt( text, srcLang, destLang );
	}
}

function translateIt( text, srcLang, destLang ) {
	var url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + text + "&langpair=" + srcLang + "%7C" + destLang;
	var req = new XMLHttpRequest();
   	req.onreadystatechange = function() {
   		if ( req.readyState == 1 ) {
			document.getElementById( 'translation' ).innerHTML = "Loading...";
		}
        if ( req.readyState == 4 ) {
			var res = eval('(' + req.responseText + ')');
			if ( res.responseData.translatedText ) {
				document.getElementById( 'translation' ).innerHTML = res.responseData.translatedText;
			}
        }
    };
    req.open( 'GET', url, true );
	req.setRequestHeader( 'Referer', 'http://www.dhruvasagar.net/' );
    req.send( null );
}

function swap() {
	var srcLang = document.getElementById( 'srcLang' ).value;
	var destLang = document.getElementById( 'destLang' ).value;
	if ( srcLang != "detect" ) {
		document.getElementById( 'srcLang' ).value = destLang;
		document.getElementById( 'destLang' ).value = srcLang;
	}
}

function loadOptions(){
	var languages = {
		'ARABIC': 'ar',
		'BULGARIAN': 'bg',
		'CATALAN': 'ca',
		'CHINESE': 'zh',
		'CHINESE_SIMPLIFIED': 'zh-CN',
		'CHINESE_TRADITIONAL': 'zh-TW',
		'CROATIAN': 'hr',
		'CZECH': 'cs',
		'DANISH': 'da',
		'DUTCH': 'nl',
		'ENGLISH': 'en',
		'FILIPINO': 'tl',
		'FINNISH': 'fi',
		'FRENCH': 'fr',
		'GERMAN': 'de',
		'GREEK': 'el',
		'HEBREW': 'iw',
		'HINDI': 'hi',
		'INDONESIAN': 'id',
		'ITALIAN': 'it',
		'JAPANESE': 'ja',
		'KOREAN': 'ko',
		'LATVIAN': 'lv',
		'LITHUANIAN': 'lt',
		'NORWEGIAN': 'no',
		'POLISH': 'pl',
		'PORTUGUESE': 'pt-PT',
		'ROMANIAN': 'ro',
		'RUSSIAN': 'ru',
		'SERBIAN': 'sr',
		'SLOVAK': 'sk',
		'SLOVENIAN': 'sl',
		'SPANISH': 'es',
		'SWEDISH': 'sv',
		'UKRAINIAN': 'uk',
		'VIETNAMESE': 'vi',
	};
	var langs;
	for(key in languages) {
		langs += "<option value='" + languages[key] + "'>" + key + "</option>";
	}
	document.getElementById( 'destLang' ).innerHTML = langs;
	document.getElementById( 'destLang' ).value = "en";
	langs = "<option value='detect'>Detect Language</option>" + langs;
	document.getElementById( 'srcLang' ).innerHTML = langs;
}